package api

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/cgs/app/models"
	"cgs-server/server/cgs/app/service"
	"cgs-server/server/cgs/app/store"
	"go.mongodb.org/mongo-driver/bson"
	"net/http"
)

type PublicWorksheetHandler struct {
	// srv service
	srv service.Service
	// store
	store store.Factory
}

func NewPublicWorksheetHandler(store store.Factory) *PublicWorksheetHandler {
	return &PublicWorksheetHandler{
		srv:   service.NewService(store),
		store: store,
	}
}

// GenerateQRCode 生成二维码
func (wg *PublicWorksheetHandler) GenerateQRCode(w http.ResponseWriter, r *http.Request) {
	content := r.FormValue("content")
	if content == "" {
		_, _ = helper.WriteJSON(w, server.Fail("content is required"))
		return
	}
	code, err := wg.srv.PublicWorksheet().GenerateQRCode(content)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(bson.M{
		"path": code,
	}))
}

func (wg *PublicWorksheetHandler) Get(w http.ResponseWriter, r *http.Request) {
	shareId := r.FormValue("shareId")
	worksheetId := r.FormValue("worksheetId")
	if shareId == "" && worksheetId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("share id or worksheet id is required"))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	var pw *models.PublicWorksheet
	if worksheetId != "" {
		ws, err := wg.srv.PublicWorksheet().GetByWorksheetId(worksheetId, user)
		if err != nil {
			_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
			return
		}
		pw = ws
	} else {
		ws, err := wg.srv.PublicWorksheet().Get(shareId, user)
		if err != nil {
			_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
			return
		}
		pw = ws
	}

	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(pw))
}

func (wg *PublicWorksheetHandler) Update(w http.ResponseWriter, r *http.Request) {
	var wsInfo models.PublicWorksheet
	err := helper.ReadRequestBody(&wsInfo, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	if wsInfo.WorksheetId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("worksheet id is required"))
		return
	}

	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.PublicWorksheet().Update(&wsInfo, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *PublicWorksheetHandler) AddExtUrl(w http.ResponseWriter, r *http.Request) {
	var extUrl models.ExtUrl
	err := helper.ReadRequestBody(&extUrl, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	if extUrl.WorksheetId == "" {
		_, _ = helper.WriteJSON(w, server.Fail("worksheet id is required"))
		return
	}

	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	res, err := wg.srv.PublicWorksheet().SaveExtUrl(&extUrl, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(res))
}

func (wg *PublicWorksheetHandler) DeleteExtUrl(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")
	if id == "" {
		_, _ = helper.WriteJSON(w, server.Fail("id is required"))
		return
	}

	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.PublicWorksheet().DeleteExtUrl(id, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}
